wellcome to Afeka's 3rd cycle of Computer Vision Course. TBD
It's common to work either on a local machine or on a portable machine on the clound.
It's up to you to chose to work either with google colab or with a local environment manged using anaconda / miniconda. (refer to the installation guide) The Common providers of portable machines are:
Installation Guide Anaconda local environment
TODOs
if you are working locally you can ignore this section and skip to imports.
from google.colab import drive
drive.mount('/content/drive')
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
import os
GOOGLE_DRIVE_PATH_AFTER_MY_DRIVE = 'IdoDrive/documents/Afeka/ComputerVision1/Lab 0'
GOOGLE_DRIVE_PATH = os.path.join('drive','My Drive',GOOGLE_DRIVE_PATH_AFTER_MY_DRIVE)
print(os.listdir(GOOGLE_DRIVE_PATH))
['Download jupyter notebook different formats.JPG', '.ipynb_checkpoints', 'demo_RGB_image.png', 'zebra.jpg', 'messi.jpg', 'Lab 0.ipynb', 'Lab_0_colab.pdf', 'Lab_0_colab.ipynb']
import sys
sys.path.append(GOOGLE_DRIVE_PATH)
import required libraries for this notebook.
import cv2
from matplotlib import pyplot as plt
from PIL import Image
%matplotlib inline
6
in order to load an image using opencv the method imread is used with the relative path name of the image to be loaded.
# demo_image_path = os.path.join(GOOGLE_DRIVE_PATH,'demo_RGB_image.png')
cv2_demo_image = cv2.imread('zebra.jpg')
Let's see some info about the image:
type() - as you can see in the output of the cell below, Opencv uses a numpy array to represent the image.shape - the image dimensions are it's height by it's width by it's amount of channels. size - holds the product of image width by image height by the amount of channels dtype - type of array.print('type:{} dimensions: {}[Height, Width, Channel] size:{} [pixels], dtype:{}'
.format(type(cv2_demo_image), cv2_demo_image.shape,
cv2_demo_image.size, cv2_demo_image.dtype))
type:<class 'numpy.ndarray'> dimensions: (798, 1200, 3)[Height, Width, Channel] size:2872800 [pixels], dtype:uint8
different libraries assumes different order of channels.
in the code cell below it's demonstrated that open-cv loads the image with the following channels order: Blue Green Red,
while matplotlib assumes a channel order of Red Green Blue.
plt.imshow(cv2_demo_image)
<matplotlib.image.AxesImage at 0x7fb05c47b6d0>
modifiy the cell below to display the difference between channels order
## Your Code Starts Here:
meaningful_name = cv2.imread('messi.jpg')
plt.imshow(meaningful_name)
## Done!
<matplotlib.image.AxesImage at 0x7fb05d7ac190>
Open-cv allows to switch between color represatation using it's
cvtColor method.
This method will be used to convert the demo_image channel representation from BGR2RGB.
demo_image_rgb = cv2.cvtColor(cv2_demo_image, cv2.COLOR_BGR2RGB)
plt.imshow(demo_image_rgb)
<matplotlib.image.AxesImage at 0x7fb05d804b90>
convert the color representation of the image you chose in the previous section from BGR to RGB
# Your code
in this section you will see how to convert a color image to a grayscale image and display them with titles side by side
demo_image_gray = cv2.cvtColor(cv2_demo_image, cv2.COLOR_BGR2GRAY)
plt.subplot(131)
plt.imshow(demo_image_rgb)
plt.title('input color image')
plt.axis('off')
plt.subplot(132)
plt.imshow(demo_image_gray)
plt.title('default view image')
plt.axis('off')
plt.subplot(133)
plt.imshow(demo_image_gray,cmap=plt.cm.gray)
plt.title(' indicate gray cmap')
plt.axis('off')
(-0.5, 1199.5, 797.5, -0.5)
type(demo_image_gray)
numpy.ndarray
different imgage processing libraries might represent images diffrently from each other. In the cell beolw we will compare between PIL and open-cv.
While open-cv uses np array to represent an image, PIL has it's own way of representing images.
pil_demo_imge_png = Image.open('demo_RGB_image.png')
pil_demo_image_jpg = Image.open('messi.jpg')
# pil_demo_image_jpg = Image.open(os.path.join(GOOGLE_DRIVE_PATH, 'messi.jpg'))
type() - as you can see in the output of the cell below, PIL uses it's own different types to represent images,and it's dependent on the format in which the image has been saved to the memory with. size - the image dimensions are it's height by it's width. mode - the color representation of the image print('type: {} dimensions: {}[H, W] mode:{}'.format(type(pil_demo_imge_png), pil_demo_imge_png.size, pil_demo_imge_png.mode ))
print('type: {} dimensions: {}[H, W] mode:{}'.format(type(pil_demo_image_jpg), pil_demo_image_jpg.size, pil_demo_image_jpg.mode ))
type: <class 'PIL.PngImagePlugin.PngImageFile'> dimensions: (220, 220)[H, W] mode:RGB type: <class 'PIL.JpegImagePlugin.JpegImageFile'> dimensions: (548, 342)[H, W] mode:RGB
As you can see PIL keeps track of the way the format in which is the image has been saved with in the machine memory
plt.imshow(pil_demo_imge_png)
<matplotlib.image.AxesImage at 0x7fb05d7f8b10>
import sys
print(sys.executable)
import sys
print(sys.executable) ß
/opt/anaconda3/envs/CV2021B/bin/python
## 3. Using open-cv draw methods to draw rectangles around messi and around the ball
messi3 = cv2.imread('messi.jpg')
type(messi3)
messi3 = cv2.cvtColor(messi3, cv2.COLOR_BGR2RGB)
plt.imshow(messi3)
## Done!
# demo_image_gray = cv2.cvtColor(cv2_demo_image, cv2.COLOR_BGR2GRAY)
messi3 = cv2.rectangle(messi3,(70,50),(470,338),(0,255,0),2)
messi3 = cv2.rectangle(messi3,(340,290),(385,330),(255,0,0),2)
plt.imshow(messi3)
# messi3 = cv2.rectangle(messi3,(70,50),(470,338),(0,255,0),2)
<matplotlib.image.AxesImage at 0x7fb062072650>
# # 4. Using open-cv add text above each rectangle you had drawn in the previous exercise to describing the content of rectangle
# font
fontBall = cv2.FONT_HERSHEY_SIMPLEX
fontPlayer = cv2.FONT_HERSHEY_SIMPLEX
# org
orgBall = (330, 290)
orgPlayer = (70,40)
# fontScale
fontScale = 1
# Blue color in BGR
colorBall = (255, 0, 0)
colorPlayer = (0, 255, 0)
# Line thickness of 2 px
thicknessBall = 2
thicknessPlayer = 2
# Using cv2.putText() method
messi3 = cv2.putText(messi3, 'ball', orgBall, fontBall,
fontScale, colorBall, thicknessBall, cv2.LINE_AA)
messi3 = cv2.putText(messi3, 'Player', orgPlayer, fontPlayer,
fontScale, colorPlayer, thicknessPlayer, cv2.LINE_AA)
# messi3 = cv2.text(messi3,(100,100),)
plt.imshow(messi3)
<matplotlib.image.AxesImage at 0x7fb0623f17d0>
import numpy as np
## 5. Append empty row below / above the image of messi
messi3.shape # : (342, 548, 3)
blank_space = np.zeros((30, 548, 3), np.int32)
messi5 = np.concatenate((messi3, blank_space), axis=0)
#messi5 = np.concatenate((blank_space,messi3, blank_space), axis=0)
plt.imshow(messi5)
<matplotlib.image.AxesImage at 0x7fb0472aa6d0>
# 6. Add text in the empy area you added to describe the image (bouns for creativity)
messi6 = cv2.putText(messi5, 'creative thought', (150,363), fontBall,
fontScale, (50,100,255), thicknessBall, cv2.LINE_AA)
plt.imshow(messi6)
<matplotlib.image.AxesImage at 0x7fb045595910>
#7. Convert the image you have read with numpy to PIL format
from PIL import Image
import numpy as np
messi7 = messi6.copy()
# PIL_image = Image.fromarray(np.uint8(numpy_image)).convert('RGB') (Already done)
PIL_image = Image.fromarray(messi7.astype('uint8'), 'RGB')
plt.imshow(PIL_image)
<matplotlib.image.AxesImage at 0x7fb048b77110>
#8. Read the zebra imge (presented below) with PIL resize(how ever you chose to) it's dimensions
# to be two times smaller and convert it to numpy format (the format opencv uses to represent images)
# a. importing as PIL
zebra8 = Image.open('zebra.jpg')
display(zebra8)
display(zebra8.size) # (1200, 798)
# b. convert to smaller size
size = (600,399) # half the dimentions
zebra8.thumbnail(size,Image.ANTIALIAS)
display(zebra8)
display(zebra8.size)
#c. convert to numpy array :)
zebra_np=np.array(zebra8)
zebra_np.shape
(1200, 798)
(600, 399)
(399, 600, 3)